<template>
  <div class="wrap-right">
    <div class="txt">所有设备</div>
    <!-- 搜索 -->
    <div class="search-box">
      <el-input
        :placeholder="
          select == 1
            ? '请输入设备序列号'
            : select == 2
            ? '请输入设备别称'
            : '请输入'
        "
        v-model="input"
        class="input-with-select"
      >
        <el-select
          v-model="select"
          style="width: 130px;"
          slot="prepend"
          placeholder="请选择"
        >
          <el-option label="设备序列号" value="1"></el-option>
          <el-option label="设备别称" value="2"></el-option>
        </el-select>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="fnSearch"
        ></el-button>
        <i
          slot="suffix"
          class="el-input__icon el-icon-circle-close"
          @click="FnAll()"
        ></i>
      </el-input>
    </div>
    <!-- 列表 -->
    <el-table
      ref="table"
      :data="tableData"
      border
      style="width: 100%"
      stripe
      @selection-change="handleSelectionChange"
      @sort-change="changeSort"
      :row-key="getRowKeys"
    >
      <el-table-column
        type="selection"
        width="40"
        align="center"
        :reserve-selection="false"
      ></el-table-column>

      <el-table-column
        prop="alias"
        label="设备名称"
        min-width="80"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.alias ? decodeURI(scope.row.alias) : "-"
        }}</template>
      </el-table-column>
      <el-table-column
        prop="sn"
        label="设备序列号"
        min-width="180"
        align="center"
      >
        <template slot-scope="scope">{{ decodeURI(scope.row.sn) }}</template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      right
      background
      layout="total,prev, pager, next, sizes,jumper"
      :page-sizes="[5, 10, 15, 25]"
      :page-size="pagesize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>

    <!--删除全部设备  -->
    <div class="batch-btn-con">
      <el-button
        class="batchDelect"
        type="primary"
        plain
        @click="batchFn(tableChecked)"
        icon="el-icon-d-arrow-left"
      ></el-button>
    </div>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
import bus from "@/utils/bus";
export default {
  name: "",
  props: {
    schedule_id: {
      type: String,
      default: "",
    },
    desc: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      select: "", //搜索中select
      input: "", //搜索中input
      currpage: 1, //当前页
      pagesize: 5, //每页几条
      total: 0, // 总数
      tableData: [], // 表格数据
      tableChecked: [], // 多选
      batchDelectId: [], //批量删除id
      checkAll: false, // 是否全选状态
    };
  },
  computed: {
    //刷新table
    submitData() {
      return this.$store.state.submitData;
    },
  },
  watch: {
    //刷新table
    submitData(newVal, oldVal) {
      if (newVal != oldVal) {
        //this.search();
        this.currpage = 1;
      }
    },
  },
  created() {},
  mounted() {
    console.log(this.schedule_id);
    this.search();
    bus.$on("refreshRight", (e) => {
      this.search();
    });
  },
  methods: {
    // 搜索
    async fnSearch() {
      let { input, select } = this;
      this.currpage = 1;

      if (input == "") {
        this.$message.error("请输入内容");
        return;
      }
      this.search();
    },
    //初始列表
    search() {
      let dataParams = {};
      if (this.select == 1) {
        dataParams = {
          page: this.currpage,
          page_num: this.pagesize,
          sn_like: this.input,
        };
      } else if (this.select == 2) {
        dataParams = {
          page: this.currpage,
          page_num: this.pagesize,
          name_like: this.input,
        };
      } else {
        dataParams = {
          page: this.currpage,
          page_num: this.pagesize,
        };
      }
      postData("/adminweb/sys/admin/device/list", dataParams)
        .then((res) => {
          if (res.code == 0) {
            if (!res.datas || !res.datas.devices) {
              return;
            }
            let devices = res.datas.devices;
            this.tableData = devices;
            this.total = res.datas.total_count;
            console.log(this.tableData);
          } else {
            //  this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          //  this.$message.error(res.code + "，" + res.msg);
        });
    },

    //批量添加，为分组添加设备
    batchFn(rows) {
      var that = this;
      console.log(that.batchDelectId);
      let devices_submit = [];
      let devices_arr = that.batchDelectId;
      
      for (let i = 0; i < devices_arr.length; i++) {
          let obj = {};
        obj.sn = devices_arr[i];
        obj.channel = "0";
        devices_submit.push(obj);
      }

      console.log(devices_submit);
      if (that.batchDelectId.length == 0) {
        that.$message({
          type: "success",
          message: "请勾选要添加的设置!",
        });
        return;
      } else {
        that
          .$confirm("此操作将批量追加设备, 是否继续?", "批量追加设备", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            postData("/adminweb/mem/schedule/device/add", {
              schedule_id: that.schedule_id,
              devices: devices_submit,
            })
              .then((res) => {
                console.log(res);
                if (res.code == "0") {
                  that.$message.success("添加成功！")
                  that.search(); // 刷新当前列表
                  //刷新左侧列表
                  bus.$emit("refreshLeft");
                } else {
                  // that.$message.error(res.code + "，" + res.msg);
                }
              })
              .catch((res) => {
                // that.$message.error(res);
              });
          })
          .catch(() => {
            //  that.$message.error("error");
          });
      }
    },
    //全部
    FnAll() {
      this.currpage = 1;
      this.input = "";
      this.select = "";
      this.search();
    },
    // 获取row的key值
    getRowKeys(row) {
      return row.schedule_id;
    },
    //点击 第几页
    handleCurrentChange(cpage) {
      this.currpage = cpage;
      this.search();
    },
    //每页有几条
    handleSizeChange(psize) {
      this.currpage = 1;
      this.pagesize = psize;
      this.search();
    },
    //操作多选
    handleSelectionChange(val) {
      console.log(val);
      var that = this;
      this.batchDelectId = [];
      that.tableChecked = val;
      console.log(that.tableChecked);
      if (val.length > 0) {
        for (let i = 0; i < val.length; i++) {
          that.batchDelectId.push(val[i]["sn"]);
        }
      }
    },
    //排序
    changeSort(val) {
      console.log(val);
    },
  },
};
</script>
<style scoped>
.search-box {
  margin: 15px 0;
  width: 100%;
}
/* 更改table,表头背景 */
.wrap-right >>> .el-table--border th,
.el-table__fixed-right-patch {
  /* background: #c0dee2; */
  background: #f0f3f4;
}
.wrap-right {
  height: 100%;
  background-color: #fff;
  padding: 15px;
  padding-top: 0;
  min-height: 100%;
  overflow: scroll;
}
.txt {
  height: 50px;
  line-height: 50px;
  font-weight: 600;
  color: #409eff;
}
.searchBox {
  margin-bottom: 20px;
}
.wrap-right >>> .el-pagination {
  float: right;
  margin-top: 30px;
}

.batch-btn-con {
  z-index: 999;
  background: #fff;
  position: absolute;
  top: 50%;
  left: -130px;
}
.wrap-right >>> .tips {
  margin-top: 10px;
  display: block;
  font-size: 15px;
}
</style>
